home *** CD-ROM | disk | FTP | other *** search
/ Aminet 50 / Aminet 50 (2002)(GTI - Schatztruhe)[!][Aug 2002].iso / Aminet / text / edit / tecoc-146.lha / sserch.c < prev    next >
C/C++ Source or Header  |  1991-07-05  |  3KB  |  117 lines

  1. /*****************************************************************************
  2.  
  3.     SSerch()
  4.  
  5.     This function does a "simple" search.  It is passed a direction
  6. and the starting and ending addresses of the contiguous area to be searched.
  7. It sets Matchd, which indicates whether the search was successful or not.
  8. If it was successful, EBPtr2 is left pointing to the last character of the
  9. found string.  Note that if the direction is backwards, the address of the
  10. beginning of the search area will be greater than the address of the end of
  11. the search area.
  12.  
  13. *****************************************************************************/
  14.  
  15. #include "zport.h"        /* define portability identifiers */
  16. #include "tecoc.h"        /* define general identifiers */
  17. #include "defext.h"        /* define external global variables */
  18.  
  19. DEFAULT SSerch()        /* search for 1 occurrence of a string */
  20. {
  21.     BOOLEAN    SamChr;        /* same character indicator */
  22.  
  23. #if DEBUGGING
  24.     static char *DbgFNm = "SSerch";
  25.     sprintf(DbgSBf,"SIncrm = %ld, EBPtr1 = %ld, EndSAr = %ld",
  26.         SIncrm, Zcp2ul(EBPtr1), Zcp2ul(EndSAr));
  27.     DbgFEn(3,DbgFNm,DbgSBf);
  28.     sprintf(DbgSBf,"search string SBf = \"%.*s\"",
  29.         (int)(SBfPtr-SBfBeg), SBfBeg);
  30.     DbgFMs(3,DbgFNm,DbgSBf);
  31. #endif
  32.  
  33.     if (SBfPtr == SBfBeg) {            /* if null search string */
  34.         Matchd = FALSE;
  35.         DBGFEX(3,DbgFNm,"SUCCESS, Matchd = FALSE");
  36.         return SUCCESS;
  37.     }
  38.  
  39.     FOREVER {
  40.         SStPtr = SBfBeg;
  41.  
  42. /*
  43.  * find a character which matches the first
  44.  * character of the search string
  45.  */
  46.  
  47.         if (SIncrm == 1) {        /* if forward search */
  48.             if (ZFrSrc() == FAILURE) {
  49.                 DBGFEX(3,DbgFNm,"FAILURE, ZFrSrc() failed");
  50.                 return FAILURE;
  51.             }
  52.             if (EBPtr1 > EndSAr) {        /* if not found */
  53.                 Matchd = FALSE;
  54.                 DBGFEX(3,DbgFNm,"SUCCESS, Matchd = FALSE");
  55.                 return SUCCESS;
  56.             }
  57.         } else {
  58.             if (BakSrc() == FAILURE) {
  59.                 DBGFEX(3,DbgFNm,"FAILURE, BakSrc() failed");
  60.                 return FAILURE;
  61.             }
  62.             if (EBPtr1 < EndSAr) {        /* if not found */
  63.                 Matchd = FALSE;
  64.                 DBGFEX(3,DbgFNm,"SUCCESS, Matchd = FALSE");
  65.                 return SUCCESS;
  66.             }
  67.         }
  68.  
  69. /*
  70.  * first char is matched,  now check rest of search string
  71.  */
  72.  
  73.         do {
  74.             if (++SStPtr == SBfPtr) { /* if no more to check */
  75.                 RefLen = (EBPtr1 - EBPtr2) - 1;
  76.                 if ((SIncrm == -1) && (EBPtr2 >= GapEnd)) {
  77.                     RefLen += GapEnd-GapBeg+1;
  78.                 }
  79.                 Matchd = TRUE;
  80.                 DBGFEX(3,DbgFNm,"SUCCESS, Matchd = TRUE");
  81.                 return SUCCESS;
  82.             }
  83.             if (++EBPtr2 > RhtSid) { /* inc and check for eob */
  84.                 if (SIncrm == -1) {
  85.                 if (EBPtr2 < GapEnd) EBPtr2 = GapEnd+1;
  86.                 } else {
  87.                 Matchd = FALSE;
  88.                 DBGFEX(3,DbgFNm,"SUCCESS, Matchd = FALSE");
  89.                 return SUCCESS;
  90.                 }
  91.             }
  92.             if (CMatch(&SamChr) == FAILURE) {
  93.                 DBGFEX(3,DbgFNm,"FAILURE, CMatch() failed");
  94.                 return FAILURE;
  95.             }
  96.         } while (SamChr);
  97.  
  98. /*
  99.  * string only partially matched,  so keep looking
  100.  */
  101.  
  102.         if (SIncrm == 1) {        /* if forward search */
  103.             if (++EBPtr1 > EndSAr) {
  104.                 Matchd = FALSE;
  105.                 DBGFEX(3,DbgFNm,"SUCCESS, Matchd = FALSE");
  106.                 return SUCCESS;
  107.             }
  108.         } else {
  109.             if (--EBPtr1 < EndSAr) {
  110.                 Matchd = FALSE;
  111.                 DBGFEX(3,DbgFNm,"SUCCESS, Matchd = FALSE");
  112.                 return SUCCESS;
  113.             }
  114.         }
  115.     } /* end of FOREVER loop */
  116. }
  117.